package com.test.webdemo.controller;

import com.test.webdemo.entity.User;
import com.test.webdemo.service.UserService;
import com.test.webdemo.servlet.RequestTracker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserById.dhtml")
    public String getUserById(Model model,String id) {
        User user = userService.getUserById(id);
        RequestTracker.logStep("6. 调用UserService.getUserById()");
        model.addAttribute("user", user);
        return "/addUser";
    }

    @RequestMapping("/addUser.dhtml")
    public String addUser(Model model, String id) {
        User user = new User();
        user.setId(id);
        user.setUsername(id);
        user.setPassword(id);
        user.setEmail(id + "@qq.com");
        user.setRole("1");
        user.setIsActive("1");
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userService.addUser(user);
        model.addAttribute("user", user);
        return "/addUser";
    }

    @RequestMapping("/webSocketTest.dhtml")
    public String webSocketTest(String id) {
        return "/WebSocketText";
    }

    @GetMapping("/showContext")
    public String showContext(HttpServletRequest request) {
        return "当前上下文路径: " + request.getContextPath();
    }


}
